package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.Product;
import com.zrrd.yunchmall.product.service.IProductService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品服务-商品管理模块")
public class ProductController {
    @Autowired
    private IProductService productService;

    @ApiOperation("查询商品列表")
    @GetMapping("/list")
    public ResponseResult list(@ApiParam("商品名称关键字") String keyword,
                               @RequestParam int pageNum,
                               @RequestParam int pageSize,
                               @ApiParam("上架状态") Integer publishStatus,
                               @ApiParam("审核状态") Integer verifyStatus,
                               @ApiParam("商品编码") String priductSn,
                               @ApiParam("商品类别ID") Integer productCategoryId,
                               @ApiParam("品牌ID") Integer brandId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(keyword))
            queryWrapper.like("name", keyword);
        if (publishStatus != null)
            queryWrapper.eq("publish_status", publishStatus);
        if (verifyStatus != null)
            queryWrapper.eq("verify_status", verifyStatus);
        if (!StringUtils.isEmpty(priductSn))
            queryWrapper.eq("product_sn", priductSn);
        if (productCategoryId != null)
            queryWrapper.eq("product_category_id", productCategoryId);
        if (brandId != null)
            queryWrapper.eq("brand_id", brandId);
        return new ResponseResult(200,"查询成功",
                productService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }


    @ApiOperation("删除商品")
    @PostMapping("/update/deleteStatus")
    public ResponseResult updateDeleteStatus(@ApiParam("删除状态") @RequestParam int deleteStatus,@RequestParam String ids){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("delete_status",deleteStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult(200,"修改成功");
    }

    @ApiOperation("上架商品")
    @PostMapping("/update/publishStatus")
    public ResponseResult updatePublishStatus(@ApiParam("上架商品") @RequestParam int publishStatus ,@RequestParam String ids){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("publish_status",publishStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult(200,"修改成功");
    }

    @ApiOperation("设为新品")
    @PostMapping("/update/newStatus")
    public ResponseResult updateNewStatus(@ApiParam("设为新品") @RequestParam int newStatus ,@RequestParam String ids){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("new_status",newStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult(200,"修改成功");
    }
    @ApiOperation("设为推荐")
    @PostMapping("/update/recommendStatus")
    public ResponseResult recommendStatus(@ApiParam("设为推荐") @RequestParam int recommendStatus ,@RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("recommand_status", recommendStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }
    @ApiOperation("新增商品信息")
    @PostMapping("/create")
    private ResponseResult create(@RequestBody Product product){
        productService.save(product);
        return new ResponseResult(200,"添加成功");
    }
    @GetMapping("/testSleuth")
    public String testSleuth() {
        return "商品：测试链路追踪";
    }

}
